package com.xy.common.util;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.xy.p2p.model.Ttenderrecord;
import com.xy.p2p.model.TtenderrecordForTransfer;

public class ErFen {
	public static void main(String[] args) {
		// principal(10000, 0.12 / 12, 12);
		// disposable(2012, 10000, 0.12, 20);
		// disposable(2013, 10000, 0.12, 20);
		// monthly
		for (Map<String, Double> m : principal(10000, 0.12 / 12, 12)) {
			System.out.println(m.get("payPerMonth") + ";" + m.get("principalMonth") + ";" + m.get("interestMonth") + ";" + (m.get("Mouth")));
		}
		System.out.println("sdfasdf");
		// java.util.Calendar c=java.util.Calendar.getInstance();
		// java.text.SimpleDateFormat f=new
		// java.text.SimpleDateFormat("yyyy年MM月dd日");
		// System.out.println(f.format(c.getTime()));
		// System.out.println(ErFen.getRamdonNum(6));
	}

	// Date d = new Date();
	// Calendar c = Calendar.getInstance();
	// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	// // c.set(Calendar.YEAR, d.getDate());
	// // c.set(Calendar.MONTH, d.getMonth());
	// c.set(Calendar.DATE, d.getDate());
	// c.add(Calendar.MONTH, +1);
	// c.add(Calendar.DAY_OF_MONTH, -15);
	// // System.out.println(sdf.format(c.getTime()));
	// // System.out.println(c.getTime());
	// Date da = new Date();
	// da.setYear(2013);
	// System.out.print(ErFen.getDaySub(c.getTime()));
	// }
	/**
	 * 等额本息
	 * 
	 * @param p
	 *            本金
	 * @param r
	 *            月利率
	 * @param n
	 *            还款月数 例如:12 (12个月) payPerMonth 月还本息 principalMonth 月还本金
	 *            interestMonth 月还利息 lastBalance 未还余额
	 * @return map集合; payPerMonth 月还本息; principalMonth 月还本金; interestMonth
	 *         月还利息;Mouth 月数
	 */
	public static List<Map<String, Double>> principal(double p, double r, int n) {
		double payPerMonth = 0.0;
		double lastBalance = p;
		double interestMonth = 0.0;
		double principalMonth = 0.0;
		double residue = p;
		DecimalFormat df = new DecimalFormat("#.00");
		List<Map<String, Double>> list = new ArrayList<Map<String, Double>>();
		for (int i = 1; i <= n; ++i) {
			payPerMonth = Double.valueOf(df.format(p * r * Math.pow((1 + r), n) / (Math.pow((1 + r), n) - 1)));
			lastBalance = Double.valueOf(df.format(lastBalance - principalMonth));
			interestMonth = Double.valueOf(df.format(lastBalance * r));
			principalMonth = payPerMonth - interestMonth;
			if (i == n) {
				principalMonth = residue;
				payPerMonth = interestMonth + principalMonth;
				residue = 0;
			} else {
				residue = Double.valueOf(df.format(residue - principalMonth));
			}
			Map<String, Double> map = new HashMap<String, Double>();
			map.put("Mouth", i + 0.0);
			map.put("payPerMonth", Double.valueOf(df.format(payPerMonth)));
			map.put("principalMonth", Double.valueOf(df.format(principalMonth)));
			map.put("interestMonth", Double.valueOf(df.format(interestMonth)));
			map.put("residue", Double.valueOf(df.format(residue)));
			list.add(map);
		}
		return list;
	}


	/**
	 * 计算日利息
	 * 
	 * @param p
	 *            本金
	 * @param r
	 *            年利率
	 * @param n
	 *            还款日 例如:5 (5日) interest 利息
	 */
	public static double disposable(double p, double r, long n) {
		// double interest = 0.0;
		//
		// if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
		// //是闰年
		// interest = p*r/366*n;
		// System.out.println("共还本息："+(p+interest)+"利息："+interest);
		// }else{
		// //不是闰年
		// interest = p*r/365*n;
		// System.out.println("共还本息："+(p+interest)+"利息："+interest);
		// }
		DecimalFormat df = new DecimalFormat("#.00");
		return Double.valueOf(df.format(p * r / 365 * n));
	}

	/**
	 * 计算上一还款日到当天的天数
	 * 
	 * @param 下一还款日
	 * @return
	 */
	private static long getDaySub(Date beginDate) {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// c.set(Calendar.YEAR, d.getDate());
		// c.set(Calendar.MONTH, d.getMonth());
		c.set(Calendar.DATE, beginDate.getDate());
		c.add(Calendar.MONTH, -1);
		long day = 0;
		try {
			System.out.println(c.getTime());
			day = (new Date().getTime() - c.getTime().getTime()) / (24 * 60 * 60 * 1000);
			// System.out.println("相隔的天数="+day);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return day;
	}

	/**
	 * @param p
	 *            本金
	 * @param r
	 *            年利率
	 * @param d
	 *            上一还款日 interest 计算上一还款日到当天的日利息
	 */
	public static double culPrice(double p, double r, Date d) {
		double resule = 0;
		Long day = getDaySub(d);
		resule = disposable(p, r, day);
		DecimalFormat df = new DecimalFormat("#.00");
		return Double.valueOf(df.format(resule));
	}

	/**
	 * 获取指定位数的随机数
	 * 
	 * @param 指定返回多少位随机数
	 * @return
	 */
	public static int getRamdonNum(int n) {
		int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		Random rand = new Random();
		for (int i = 10; i > 1; i--) {
			int index = rand.nextInt(i);
			int tmp = array[index];
			array[index] = array[i - 1];
			array[i - 1] = tmp;
		}
		int result = 0;
		for (int i = 0; i < n; i++)
			result = result * 10 + array[i];
		return result;
	}

	/**
	 * 每月付息到期还本
	 * 
	 * @param p
	 *            本金
	 * @param r
	 *            月利率
	 * @param n
	 *            还款月数 例如:12 (12个月) payPerMonth 月还本息 principalMonth 月还本金
	 *            interestMonth 月还利息 lastBalance 本息余额
	 * @return map集合; payPerMonth 月还本息; principalMonth 月还本金; interestMonth
	 *         月还利息;Mouth 月数; lastBalance 本息余额
	 */
	public static List<Map<String, Double>> monthly(double p, double r, int n) {
		int m = n;
		double payPerMonth = 0.0;
		double lastBalance = 0.0;
		double interestMonth = 0.0;
		double principalMonth = 0.0;
		DecimalFormat df = new DecimalFormat("#.00");
		List<Map<String, Double>> list = new ArrayList<Map<String, Double>>();
		for (int i = 1; i <= n; ++i) {
			m = m - 1;
			lastBalance = p + p * r * m;
			interestMonth = p * r;
			if (i == n) {
				principalMonth = p;
				lastBalance = 0;
			}
			payPerMonth = interestMonth + principalMonth;
			Map<String, Double> map = new HashMap<String, Double>();
			map.put("Mouth", i + 0.0);
			map.put("payPerMonth", Double.valueOf(df.format(payPerMonth)));
			map.put("principalMonth", Double.valueOf(df.format(principalMonth)));
			map.put("interestMonth", Double.valueOf(df.format(interestMonth)));
			map.put("lastBalance", Double.valueOf(df.format(lastBalance)));
			list.add(map);
		}
		return list;
	}

	/**
	 * 计算月利息
	 * 
	 * @param p
	 *            本金
	 * @param r
	 *            年利率
	 * @return
	 */
	public static double culMonthlyInterest(double p, double r) {
		DecimalFormat df = new DecimalFormat("#.00");
		return Double.valueOf(df.format(p * r / 12.00));
	}
}
